/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Classes.Immo;
import DAO.ImmoDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author alexander
 */
public class AddImmo extends HttpServlet {


   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
        
        System.out.println("Hallo, ich bins, das AddImmo Servlet");
        
        int  immoOfferType = Integer.parseInt(request.getParameter("ImmoOfferType"));
        String immoOfferTypeAsString =  "";
        if (immoOfferType==0){
            immoOfferTypeAsString="Kauf";
        }
        
        if (immoOfferType==1){
            
            immoOfferTypeAsString="Miete";
        }
        System.out.println("****************");
        System.out.println(request.getParameter("ImmoYear"));
        System.out.println("****************");
        System.out.println(request.getParameter("ImmoNumberOfRooms"));
        System.out.println("****************");
        
        
        String  immoCategory = request.getParameter("ImmoCategory");
        String  immoHeadLine = request.getParameter("ImmoHeadline");
        int     immoLivingArea = Integer.parseInt(request.getParameter("ImmoLivingArea"));
        int     immoTotalArea = Integer.parseInt(request.getParameter("ImmoTotalArea"));
        int     immoTotalNumberOfRooms = Integer.parseInt(request.getParameter("ImmoNumberOfRooms"));
        int     immoPrice = Integer.parseInt(request.getParameter("ImmoPrice"));
        String  immoDetails = request.getParameter("ImmoDetails");
        String  immoZipCode = request.getParameter("ImmoZipCode"); 
        String  immoAddresss = request.getParameter("ImmoAddress");
        String  immoState = request.getParameter("ImmoState");
        String  immoCountry= request.getParameter("ImmoCountry");
        String  immoYear = request.getParameter("ImmoYear");
      
        //String immoYear = "2002";
        
        System.out.println("Baujahr:"+immoYear);
        int     immoYearAsInteger = Integer.parseInt(immoYear);
        
        String searchString =   immoOfferTypeAsString+","+immoHeadLine+","+
                                immoDetails+","+immoAddresss+","+immoState+","+
                                immoCountry+","+"Räume: "+immoTotalNumberOfRooms+
                                ","+immoCategory+","+immoYear;    
        //System.out.println(searchString);
        
        Immo newImmo = new Immo();
        newImmo.setImmoSaleOrRent(immoOfferType);
        newImmo.setAddress(immoAddresss);
        newImmo.setCountry(immoCountry);
        newImmo.setImmoCategory(immoCategory);
        newImmo.setImmoDescription(immoDetails);
        newImmo.setImmoHeadline(immoHeadLine);
        if (immoCategory.equalsIgnoreCase("Häuser")){
            newImmo.setIsAHouse(true);
        }
        else {
            newImmo.setIsAHouse(false);
        }
        newImmo.setIsSold(false);
        newImmo.setLinvingArea(immoLivingArea);
        newImmo.setNumberOfRooms(immoTotalNumberOfRooms);
        newImmo.setPrice(immoPrice);
        newImmo.setSearchString(searchString);
        newImmo.setSizeOfTheArea(immoTotalArea);
        newImmo.setState(immoState);
        newImmo.setZipCode(immoZipCode);
        newImmo.setYear(immoYearAsInteger);

        ImmoDAO newImmoDAO = new ImmoDAO();
        
        newImmoDAO.saveImmo(newImmo);
        System.out.println("Check, Immobilie gespeichert");
        
        
        
        
    }

 
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
